import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  /********** 缙云融媒智汇平台  **********/
  {
    path: '/',
    redirect: '/login'
  },
  /********** 登录页  **********/
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/1.Login/Login.vue')
  },
  /********** 主页  **********/
  {
    path: '/home-page',
    name: 'home-page',
    component: () => import('@/views/HomePage/HomePage.vue')
  },
  /********** 短视频管理  **********/
  {
    path: '/main',
    name: 'main',
    component: () => import('@/views/2.Main/Main.vue'),
    redirect: '/main/home',
    children: [
      /** 工作台 **/
      {
        path: 'home',
        name: 'home',
        meta: {
          menuKey: 'home',
          breadcrumbs: ['工作台']
        },
        component: () => import('@/views/3.Home/Home.vue')
      },
      /** 投稿管理 **/
      {
        path: 'contribution-list',
        name: 'contribution-list',
        meta: {
          menuKey: 'contribution-list',
          breadcrumbs: ['投稿管理', '投稿记录管理']
        },
        component: () => import('@/views/4.Contribution/ContributionMgr.vue')
      },
      {
        path: 'contribution-add',
        name: 'contribution-add',
        meta: {
          menuKey: 'contribution-add',
          breadcrumbs: ['投稿管理', '新增投稿']
        },
        component: () => import('@/views/4.Contribution/AddContribution.vue')
      },
      /** 视频平台数据导入 **/
      {
        path: 'import-extra-data',
        name: 'import-extra-data',
        meta: {
          menuKey: 'import-extra-data',
          breadcrumbs: ['视频平台数据导入'],
          roleKey: 'role',
          requireRole: ['系统管理员', '审核员']
        },
        component: () => import('@/views/5.ReportMgr/ImportExtraData.vue')
      },
      /** 根据平台数据打分 **/
      {
        path: 'score-extra-data',
        name: 'score-extra-data',
        meta: {
          menuKey: 'score-extra-data',
          breadcrumbs: ['根据平台数据打分'],
          roleKey: 'role',
          requireRole: ['系统管理员', '审核员']
        },
        component: () => import('@/views/5.ReportMgr/ScoreExtraData.vue')
      },
      /** 报表计算 **/
      {
        path: 'import-calculate',
        name: 'import-calculate',
        meta: {
          menuKey: 'import-calculate',
          breadcrumbs: ['报表计算'],
          roleKey: 'role',
          requireRole: ['系统管理员', '审核员']
        },
        component: () => import('@/views/5.ReportMgr/Calculate.vue')
      },
      /** 打分规则管理 **/
      {
        path: 'score-rule',
        name: 'score-rule',
        meta: {
          menuKey: 'score-rule',
          breadcrumbs: ['打分规则管理'],
          roleKey: 'role',
          requireRole: ['系统管理员']
        },
        component: () => import('@/views/6.Rule/Rule.vue')
      },
      /** 项目组管理 **/
      {
        path: 'project-team',
        name: 'project-team',
        meta: {
          menuKey: 'project-team',
          breadcrumbs: ['项目组管理'],
          roleKey: 'role',
          requireRole: ['系统管理员']
        },
        component: () => import('@/views/9.ProjectGroup/ProjectGroup.vue')
      }
    ]
  },
  /********** 短视频排行榜  **********/
  {
    path: '/statistic-page',
    name: 'statistic-page',
    component: () => import('@/views/0.Statistic/Statistic.vue')
  },
  /********** 人员去向表  **********/
  {
    path: '/destination-main',
    name: 'destination-main',
    component: () => import('@/views/D2.Destination/MainLayout.vue'),
    redirect: '/destination-main/destination-view',
    children: [
      /** 录入 **/
      {
        path: 'destination-edit',
        name: 'destination-edit',
        meta: {
          menuKey: 'destination-edit',
          breadcrumbs: ['录入']
        },
        component: () => import('@/views/D2.Destination/DestinationEdit.vue')
      },
      /** 查看 **/
      {
        path: 'destination-view',
        name: 'destination-view',
        meta: {
          menuKey: 'destination-view',
          breadcrumbs: ['查看']
        },
        component: () => import('@/views/D2.Destination/DestinationView.vue')
      },
    ]
  },
  // 日历
  {
    path: '/calendarview',
    name: 'calendarview',
    component: () => import('@/views/D2.Destination/CalendarView.vue')
  },
  /********** 部门人员管理  **********/
  {
    path: '/department-main',
    name: 'department-main',
    component: () => import('@/views/DP0.Main/Main.vue'),
    redirect: '/department-main/department',
    children: [
      /** 部门管理 **/
      {
        path: 'department',
        name: 'department',
        meta: {
          menuKey: 'department',
          breadcrumbs: ['部门管理'],
          roleKey: 'role_main',
          requireRole: ['系统管理员']
        },
        component: () => import('@/views/DP1.Department/Department.vue')
      },
      /** 人员管理 **/
      {
        path: 'person-mgr',
        name: 'person-mgr',
        meta: {
          menuKey: 'person',
          breadcrumbs: ['人员管理'],
          roleKey: 'role_main',
          requireRole: ['系统管理员']
        },
        component: () => import('@/views/DP2.PersonMgr/PersonMgr.vue')
      },
    ]
  },
  /********** 系统日志  **********/
  {
    path: '/sys-log',
    name: 'sys-log',
    component: () => import('@/views/SYS0.Log/log.vue'),
  },
  /********** 公车管理  **********/
  {
    path: '/ov-main',
    name: 'ov-main',
    component: () => import('@/views/OV0.Main/Main.vue'),
    redirect: '/ov-main/vehicle-allocation',
    children: [
      /** 车辆使用情况 **/
      {
        path: 'vehicle-allocation',
        name: 'vehicle-allocation',
        meta: {
          menuKey: 'vehicle-allocation',
          breadcrumbs: ['车辆使用情况'],
        },
        component: () => import('@/views/OV1.VehicleAllocation/VehicleAllocation.vue')
      },
      /** 驾驶员出车情况 **/
      {
        path: 'driver-allocation',
        name: 'driver-allocation',
        meta: {
          menuKey: 'driver-allocation',
          breadcrumbs: ['驾驶员出车情况'],
        },
        component: () => import('@/views/OV2.DriverAllocation/DriverAllocation.vue')
      },
      /** 用车申请列表 **/
      {
        path: 'vehicle-application',
        name: 'vehicle-application',
        meta: {
          menuKey: 'vehicle-application',
          breadcrumbs: ['用车申请列表'],
        },
        component: () => import('@/views/OV3.VehicleApplication/VehicleApplication.vue')
      },
      /** 申请用车 **/
      {
        path: 'add-vehicle-application',
        name: 'add-vehicle-application',
        meta: {
          menuKey: 'add-vehicle-application',
          breadcrumbs: ['申请用车'],
          roleKey: 'role_ov',
          requireRole: ['部门负责人', '系统管理员']
        },
        component: () => import('@/views/OV4.AddVehicleApplication/AddVehicleApplication.vue')
      },
      /** 日常记录 **/
      {
        path: 'daily-record',
        name: 'daily-record',
        meta: {
          menuKey: 'daily-record',
          breadcrumbs: ['日常记录'],
        },
        component: () => import('@/views/OV5.DailyRecord/DailyRecord.vue')
      },
      /** 出车数据统计 **/
      {
        path: 'vehicle-statistics',
        name: 'vehicle-statistics',
        meta: {
          menuKey: 'vehicle-statistics',
          breadcrumbs: ['出车数据统计'],
          roleKey: 'role_ov',
          requireRole: ['系统管理员']
        },
        component: () => import('@/views/OV6.VehicleStatistics/VehicleStatistics.vue')
      },
    ]
  },
  /********** 报题系统  **********/
  {
    path: '/ap-main',
    name: 'ap-main',
    component: () => import('@/views/AP0.Main/Main.vue'),
    redirect: '/ap-main/ap-home',
    children: [
      /** 报题系统主页 **/
      {
        path: 'ap-home',
        name: 'ap-home',
        meta: {
          menuKey: 'ap-home',
          breadcrumbs: ['报题系统主页'],
        },
        component: () => import('@/views/AP1.Home/Home.vue')
      },
      /** 报题列表 **/
      {
        path: 'approval-list',
        name: 'approval-list',
        meta: {
          menuKey: 'approval-list',
          breadcrumbs: ['报题列表'],
        },
        component: () => import('@/views/AP2.Approval/ApprovalList.vue')
      },
      /** 提交报题 **/
      {
        path: 'add-approval',
        name: 'add-approval',
        meta: {
          menuKey: 'add-approval',
          breadcrumbs: ['提交报题'],
        },
        component: () => import('@/views/AP2.Approval/AddApproval.vue')
      },
      /** 我的提交 **/
      {
        path: 'my-applications',
        name: 'my-applications',
        meta: {
          menuKey: 'my-applications',
          breadcrumbs: ['我的提交'],
        },
        component: () => import('@/views/AP2.Approval/MyApplications.vue')
      },
      /** 我的报题 **/
      {
        path: 'applications-workmember',
        name: 'applications-workmember',
        meta: {
          menuKey: 'applications-workmember',
          breadcrumbs: ['我的报题'],
        },
        component: () => import('@/views/AP2.Approval/MyApplicationsWorkmember.vue')
      },
      /** 待审批列表 **/
      {
        path: 'pending-approver',
        name: 'pending-approver',
        meta: {
          menuKey: 'pending-approver',
          breadcrumbs: ['待审批列表'],
        },
        component: () => import('@/views/AP2.Approval/PendingApprover.vue')
      },
      /** 已审批列表 **/
      {
        path: 'approved-approver',
        name: 'approved-approver',
        meta: {
          menuKey: 'approved-approver',
          breadcrumbs: ['已审批列表'],
        },
        component: () => import('@/views/AP2.Approval/ApprovedApprover.vue')
      },
      /** 报题审核权限 **/
      {
        path: 'approval-user',
        name: 'approval-user',
        meta: {
          menuKey: 'approval-user',
          breadcrumbs: ['报题审核权限'],
        },
        component: () => import('@/views/AP2.Approval/ApprovalUser.vue')
      },
    ]
  },
  /********** A8  **********/
  {
    path: '/A8-form',
    name: 'A8-form',
    component: () => import('@/views/A8/A8From.vue')
  },
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // A8表单
  if (to.path === '/A8-form') {
    next()
    return
  }

  // 日历
  if (to.path === '/calendarview') {
    next()
    return
  }

  // 统计页面直接放行
  if (to.path === '/statistic-page') {
    next()
    return
  }

  // 登录页面直接放行
  if (to.path === '/login') {
    next()
    return
  }

  // 检查是否已登录
  const token = window.sessionStorage.getItem('token')
  if (!token) {
    next('/login')
    return
  }

  // 权限检查
  if (Array.isArray(to.meta.requireRole)) { // 如果有权限检查
    const personInfo = JSON.parse(window.sessionStorage.getItem('person_info') || '{}')
    if (to.meta.requireRole.includes(personInfo[to.meta.roleKey])) { // 通过
      next()
      return
    } else { // 不通过
      Vue.prototype.$message.error('您没有权限')
      return
    }
  }

  next()
})

export default router